package com.usian.service.impl;

import com.usian.fegin.AuthorFegin;
import com.usian.model.comment.CommentRepayDto;
import com.usian.model.comment.CommentRepayLikeDto;
import com.usian.model.comment.CommentRepaySaveDto;
import com.usian.model.comment.CommentSaveDto;
import com.usian.model.comment.pojos.ApComment;
import com.usian.model.comment.pojos.ApCommentLike;
import com.usian.model.comment.pojos.ApCommentRepay;
import com.usian.model.comment.pojos.ApCommentRepayLike;
import com.usian.model.comment.vo.ApCommentVo;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.service.CommentService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private AuthorFegin authorFeginl;

    @Override
    public ResponseResult add(CommentSaveDto dto) {

        //判断参数是否为空
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getContent()!=null && dto.getContent().length()>140){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论的内容不能超过140字");
        }
        ApUser user = AppThreadLocalUtils.getUser();
        ApUser byId = authorFeginl.findById(user.getId());

        ApComment apComment = new ApComment();
        apComment.setEntryId(dto.getArticleId());
        apComment.setContent(dto.getContent());
        apComment.setAuthorId(byId.getId());
        apComment.setAuthorName(byId.getName());
        apComment.setImage(byId.getImage()!=null?byId.getImage():"");
        apComment.setUpdatedTime(new Date());
        apComment.setCreatedTime(new Date());
        apComment.setFlag((short)0);
        apComment.setLikes(0);
        apComment.setType(0);
        apComment.setLikes(0);
        apComment.setReply(0);
        mongoTemplate.insert(apComment);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult findByUserIdAndEntryId(CommentSaveDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        //判断用户是否登录
        if(user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户没登录不能查看评论");
        }
        Query query = new Query(Criteria.where("entryId").is(dto.getArticleId()));
        List<ApComment> apComments = mongoTemplate.find(query, ApComment.class);

        ArrayList<ApCommentVo> apCommentVos = new ArrayList<>();
        for (ApComment apComment : apComments) {
            ApCommentVo apCommentVo = new ApCommentVo();
            BeanUtils.copyProperties(apComment,apCommentVo);
            Query query1 = new Query(Criteria.where("commentId").is(apComment.getId()).and("authorId").is(user.getId()));
            ApCommentLike apCommentLikes = mongoTemplate.findOne(query1, ApCommentLike.class);
            if(apCommentLikes==null){
                apCommentVo.setOperation(1);
            }else{
                apCommentVo.setOperation(0);
            }
            apCommentVos.add(apCommentVo);
        }

        return ResponseResult.okResult(apCommentVos);
    }

    @Override
    public ResponseResult loadCommentRepay(CommentRepayDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        //判断用户是否登录
        if(user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户没登录不能查看评论");
        }
        Query query = new Query(Criteria.where("commentId").is(dto.getCommentId()).and("createdTime").lt(dto.getMinDate()));
        List<ApCommentRepay> apComments = mongoTemplate.find(query, ApCommentRepay.class);

        ArrayList<ApCommentVo> apCommentVos = new ArrayList<>();
        for (ApCommentRepay apComment : apComments) {
            ApCommentVo apCommentVo = new ApCommentVo();
            BeanUtils.copyProperties(apComment,apCommentVo);
            Query query1 = new Query(Criteria.where("commentRepayId").is(apComment.getId()).and("authorId").is(user.getId()));
            ApCommentRepayLike apCommentLikes = mongoTemplate.findOne(query1, ApCommentRepayLike.class);
            if(apCommentLikes==null){
                apCommentVo.setOperation(1);
            }else{
                apCommentVo.setOperation(0);
            }
            apCommentVos.add(apCommentVo);
        }

        return ResponseResult.okResult(apCommentVos);
    }

    @Override
    public ResponseResult saveCommentRepay(CommentRepaySaveDto dto) {
        //判断参数是否为空
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getContent()!=null && dto.getContent().length()>140){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"评论的内容不能超过140字");
        }
        ApUser user = AppThreadLocalUtils.getUser();
        ApUser byId = authorFeginl.findById(user.getId());

        ApCommentRepay apComment = new ApCommentRepay();
        apComment.setCommentId(dto.getCommentId());
        apComment.setContent(dto.getContent());
        apComment.setAuthorId(byId.getId());
        apComment.setAuthorName(byId.getName());
        apComment.setUpdatedTime(new Date());
        apComment.setCreatedTime(new Date());
        apComment.setLikes(0);
        apComment.setAddress("回复评论");
        apComment.setLatitude(new BigDecimal(0));
        apComment.setLongitude(new BigDecimal(0));
        mongoTemplate.insert(apComment);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveCommentRepayLike(CommentRepayLikeDto dto) {
        //判断参数是否为空
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取用户id
        ApUser user = AppThreadLocalUtils.getUser();
        //判断是点赞还是取消点赞
        if(dto.getOperation()==0){
            //添加ApCommentLike表
            ApCommentRepayLike apCommentLike = new ApCommentRepayLike();
            apCommentLike.setOperation(dto.getOperation());
            apCommentLike.setAuthorId(user.getId());
            apCommentLike.setCommentRepayId(dto.getCommentRepayId());
            mongoTemplate.insert(apCommentLike);

            //修改点赞次数
            //完成ApComment的likes查询
            Query query = new Query(Criteria.where("Id").is(dto.getCommentRepayId()));
            ApCommentRepay one = mongoTemplate.findOne(query, ApCommentRepay.class);
            Integer likes = one.getLikes();

            //自增点赞次数
            likes++;

            //完成修改ApCommentRepay
            Update likes1 = Update.update("likes", likes);
            mongoTemplate.updateFirst(query,likes1,ApCommentRepay.class);
        }else{
            //删除ApCommentLike表
            Query query = new Query(Criteria.where("commentRepayId").is(dto.getCommentRepayId()));
            mongoTemplate.remove(query,ApCommentRepayLike.class);

            //完成ApComment的likes查询
            Query query1 = new Query(Criteria.where("Id").is(dto.getCommentRepayId()));
            ApCommentRepay one = mongoTemplate.findOne(query1, ApCommentRepay.class);
            Integer likes = one.getLikes();
            //自减点赞次数
            likes--;

            //完成修改ApComment
            Update likes1 = Update.update("likes", likes);
            mongoTemplate.updateFirst(query1,likes1,ApCommentRepay.class);
        }
        return null;
    }
}
